package polygon.cli.command;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.log4j.Logger;
import polygon.cli.Command;
import polygon.cli.CommandException;
import polygon.cli.util.ProblemUtil;
import polygon.util.FileUtil;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

/**
 * @author MikeMirzayanov (mirzayanovmr@gmail.com)
 */
@SuppressWarnings({"AccessStaticViaInstance"})
public class EjudgeDeployProblemCommand extends Command {
    private static final Logger logger = Logger.getLogger(EjudgeDeployProblemCommand.class);

    public String getDescription() {
        return "(Ejudge deploy problem): deploys or updates problem in the ejudge installation";
    }

    public void setupOptions() {
        Option contestDir = OptionBuilder.withArgName("contestDir")
                .hasArg()
                .withDescription("Contest directory (example: /home/judges/010203), " +
                        "short form means directory in /home/judges (example: 010203)")
                .create("contestDir");
        getOptions().addOption(contestDir);

        Option letter = OptionBuilder.withArgName("letter")
                .hasArg()
                .withDescription("Problem letter")
                .create("letter");
        getOptions().addOption(letter);

        Option url = OptionBuilder.withArgName("url")
                .hasArg()
                .withDescription("Problem URL (example: http://polygon.server.ru:8080/p/vtapochkin/brackets")
                .create("url");
        getOptions()
                .addOption(url);
    }

    public void run() {
        Scanner scanner = new Scanner(System.in);

        String url = getCommandLine().getOptionValue("url");
        if (url == null) {
            System.out.println("Enter problem URL:");
            url = scanner.nextLine();
        }

        String contestDir = getCommandLine().getOptionValue("contestDir");
        if (contestDir == null) {
            System.out.println("Enter contest directory (press ENTER for current directory):");
            contestDir = scanner.nextLine();
            if (contestDir.equals("")) {
                contestDir = ".";
            }
        }

        String letter = getCommandLine().getOptionValue("letter");
        if (letter == null) {
            System.out.println("Enter problem letter:");
            letter = scanner.nextLine();
        }

        if (!contestDir.equals(".") && !new File(contestDir).isAbsolute()) {
            contestDir = "/home/judges/" + contestDir;
        }

        File packageFile = null;

        try {
            packageFile = ProblemUtil.download(new URL(url), getCli().getLogin(), getCli().getPassword());
            ProblemUtil.ejudgeDeployOrUpdateProblem(
                    packageFile,
                    logger,
                    new File(contestDir),
                    letter
            );
        } catch (Exception e) {
            throw new CommandException("Can't process package.", e);
        } finally {
            if (packageFile != null) {
                try {
                    FileUtil.deleteTotaly(packageFile);
                } catch (IOException e) {
                    // No operations.
                }
            }
        }
    }
}
